import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false
// 判断市场常见的几种刘海屏机型
uni.getSystemInfo({
  success: function(res) {
    let modelmes = res.model
    console.log('手机型号-------', res.model)
    if (modelmes.indexOf('iPhone X') >= 0 || modelmes.indexOf('iPhone XR') >= 0 || modelmes.indexOf('iPhone XS') >= 0 ||
			modelmes.indexOf('iPhone 12') >= 0 || modelmes.indexOf('iPhone 11') >= 0 || modelmes.indexOf('iPhone11') >= 0 ||
			modelmes.indexOf('iPhone12') >= 0 || modelmes.indexOf('iPhoneXR') >= 0 || modelmes.indexOf('iPhoneX') >= 0) {
      Vue.prototype.$is_bang = true
    } else {
      Vue.prototype.$is_bang = false
    }
  }
})
//地图挂载
var amapFile = require('@/utils/amap-wx.130.js');
Vue.prototype.myAmapFun = new amapFile.AMapWX({
	key: 'fd510e765473ce1542045413efd1f892'
});





// 接口文档https://console-docs.apipost.cn/preview/cd9421a095980a5e/8097b1a30851bfc1

// 测试
// Vue.prototype.globalurl = 'http://ccc.healthspringny.com/api/'
Vue.prototype.globalurl = 'http://www.cccshansong.com/api/'
// Vue.prototype.globalImgurl = 'https://shijian.400539.com/storage/'


import uView from './uni_modules/uview-ui'
Vue.use(uView)

import common from './common/common'
Vue.prototype.$common = common;



Vue.prototype.globalajax = function(url, data, way, fun) {
  var token = uni.getStorageSync('token')
  // data.token = token
  uni.request({
    url: this.globalurl + url,
    data: data,
    header: {
      'Accept': 'application/json',
      'content-type': 'application/json',
      'Authorization':'Bearer ' + token
    },
    method: way,
    success: (res) => {
      uni.hideLoading()
      console.log(res);
      if (res.statusCode == 200) {
        if (res.data.code == 401) {
          uni.clearStorageSync()
		  uni.showToast({
		    title: '请先登录~',
		    icon: 'none',
		    duration: 3000
		  })
          uni.navigateTo({
            url: '/pages/index/login'
          })
        } else {
          return fun(res)
        }
      }else if(res.statusCode == 401){
			uni.clearStorageSync()
			uni.showToast({
			  title: '请先登录~',
			  icon: 'none',
			  duration: 3000
			})
			uni.navigateTo({
			  url: '/pages/user/login'
			})
	  }
    },
    fail: (err) => {
      console.log(err);
	  uni.hideLoading()
      uni.showToast({
        title: '请求超时，网络开小差了~',
        icon: 'none',
        duration: 3000
      })
    }
  })
}
/**
 * 显示失败提示框
 */
function showError(msg, callback) {
  msg = getReadableMsg(msg)
  wx.showModal({
    title: '友情提示',
    content: msg,
    showCancel: false,
    confirmText: '知道了',
    confirmColor: '#FF6B00',
    success: function(res) {
      callback && callback()
    }
  })
}
/*

 *  获取可读的msg
 */
function getReadableMsg(msg) {
  if (typeof msg === 'object') {
    msg = msg[Object.keys(msg)[0]]
    return getReadableMsg(msg)
  } else if (typeof msg === 'array') {
    return getReadableMsg(msg[0])
  } else if (typeof msg === 'string') {
    return msg
  }
}
// 在 main.js 文件中直接写方法
Vue.filter('unitConverter', (value)=>{
  if (!value) return 0
  // 获取整数部分
  const intPart = Math.trunc(value)
  // 整数部分处理，增加,
  const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
  // 预定义小数部分
  let floatPart = ''
  // 将数值截取为小数部分和整数部分
  const valueArray = value.toString().split('.')
  if (valueArray.length === 2) { // 有小数部分
    floatPart = valueArray[1].toString() // 取得小数部分
    return intPartFormat + '.' + floatPart
  }
  return intPartFormat + floatPart
})


Vue.prototype.showError = function(msg, callback) {
  uni.hideLoading()
  uni.showModal({
    title: '友情提示',
    content: msg,
    showCancel: false,
    confirmText: '知道了',
    confirmColor:  '#FF6B00',
    success: function(res) {
      callback && callback()
    }
  })
}
// 全局函数
import global_function from './common/global_function.js'
Vue.use(global_function)


App.mpType = 'app'



const app = new Vue({
  ...App
})
app.$mount()
